package com.sky.controller.admin;

import com.sky.annotation.AutoFill;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.enumeration.OperationType;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/admin/category")
@RestController
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @PutMapping()
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类");
        categoryService.update(categoryDTO);
        return Result.success();

    }
    @PostMapping()
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类");
        categoryService.add(categoryDTO);
        return Result.success();
    }
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询{}",categoryPageQueryDTO);
        PageResult pageResult=categoryService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }
    @PostMapping("status/{status}")
    public Result updateCategoryStatus(@PathVariable("status") Integer status,Long id) {
        log.info("启用禁用分类");
        categoryService.updateStatus(status,id);
        return Result.success();
    }
    @DeleteMapping("")
    public Result deleteById(long id){
        log.info("根据id删除分类");
        categoryService.deleteById(id);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result list(Integer type){
        log.info("根据类型查询分类");
        List<Category> category=categoryService.list(type);
        return Result.success(category);
    }

}
